<template>
  <div class="login-container">
    <div class="login-box">
      <div class="left">
        <img src="@/assets/pic.24b05f25.png" alt="login-bg" class="login-bg">
      </div>
      <div class="right">
        <div class="login-content">
          <h2 class="title">能耗在线监测系统</h2>
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                :prefix-icon="User"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                :prefix-icon="Lock"
                @keyup.enter="handleLogin"
              />
            </el-form-item>
            <el-form-item class="remember-me">
              <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
            </el-form-item>
            <el-form-item>
              <el-button
                :loading="loading"
                type="primary"
                class="login-button"
                @click="handleLogin"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const loginFormRef = ref<FormInstance>()
const loading = ref(false)

const loginForm = ref({
  username: '',
  password: '',
  remember: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  try {
    loading.value = true
    console.log('开始登录跳转')
    
    // 设置 token 到 localStorage
    localStorage.setItem('token', 'your-token-value')
    
    // 可以添加其他用户信息
    localStorage.setItem('userInfo', JSON.stringify({
      username: loginForm.value.username,
      // 其他用户信息...
    }))
    
    window.location.href = './dashboard'
    ElMessage.success('登录成功')
  } catch (error) {
    console.error('路由跳转错误:', error)
    ElMessage.error('登录失败，请重试')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  min-width: 1200px;
  background-color: $bg-color;
  display: flex;
  justify-content: center;
  align-items: center;
  
  .login-box {
    width: 1000px;
    height: 600px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 20px;
    display: flex;
    overflow: hidden;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    
    .left {
      flex: 1;
      background: rgba(255, 255, 255, 0.02);
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 40px;
      
      .login-bg {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    }
    
    .right {
      width: 400px;
      background: rgba(255, 255, 255, 0.03);
      padding: 40px;
      display: flex;
      align-items: center;
      
      .login-content {
        width: 100%;
        
        .title {
          font-size: 24px;
          color: $text-color;
          text-align: center;
          margin-bottom: 40px;
          font-weight: 500;
        }
        
        .login-form {
          .el-input {
            --el-input-bg-color: rgba(255, 255, 255, 0.05);
            --el-input-border-color: transparent;
            --el-input-hover-border-color: $primary-color;
            --el-input-focus-border-color: $primary-color;
            --el-input-text-color: $text-color;
            --el-input-placeholder-color: rgba(255, 255, 255, 0.3);
            
            :deep(.el-input__wrapper) {
              box-shadow: none;
              background-color: rgba(255, 255, 255, 0.05);
              border-radius: 4px;
              
              &.is-focus {
                box-shadow: 0 0 0 1px $primary-color;
              }
            }
            
            :deep(.el-input__prefix) {
              color: rgba(255, 255, 255, 0.3);
            }
          }
          
          .remember-me {
            margin-bottom: 24px;
            
            :deep(.el-checkbox__label) {
              color: $text-color;
            }
          }
          
          .login-button {
            width: 100%;
            height: 40px;
            border-radius: 4px;
            font-size: 16px;
            
            &.is-loading {
              background-color: rgba($primary-color, 0.8);
            }
          }
        }
      }
    }
  }
}
</style>
